//https://leetcode.cn/problems/minimum-genetic-mutation/

class Solution {
public:
    int minMutation(string startGene, string endGene, vector<string>& bank) {
        //存储访问过的元素
        unordered_set<string> vit;
        //存储基因库
        unordered_set<string> hash(bank.begin(), bank.end());

        if (startGene == endGene) return 0;
        if (!hash.count(endGene)) return -1;

        queue<string> q;
        q.push(startGene);
        vit.insert(startGene);
        int ret = 0;
        string changestr = "ACGT";

        while (q.size())
        {
            ret++;
            int sz = q.size();
            for (int i = 0; i < sz; i++)
            {
                string s = q.front();
                q.pop();

                for (int j = 0; j < 8; j++)
                {
                    string temp = s;
                    for (int k = 0; k < 4; k++)
                    {
                        temp[j] = changestr[k];
                        if (hash.count(temp) && !vit.count(temp))
                        {
                            if (temp == endGene) return ret;

                            q.push(temp);
                            vit.insert(temp);
                        }
                    }
                }
            }
        }

        return -1;

    }
};